package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.subscribe;
import com.itheima.service.SubscribeService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/subscribe")
public class SubscribeController {
    @Reference
    private SubscribeService subscribeService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) throws Exception {
        return subscribeService.pageQuery(queryPageBean);
    }

    /*回显套餐列表*/
    @RequestMapping("/echo")
    public Result echoSetmeal() {
        try {
            List<Setmeal> setmeal = subscribeService.echoSetmeal();
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    //添加
    @RequestMapping("/add")
    public Result addoder(@RequestBody Map formData, Integer[] id) {
        try {
            Result result = subscribeService.addoder(formData, id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

    /*编辑基本信息回显*/
    @RequestMapping("/editecho")
    public Result editecho(@RequestBody Map row) {
        try {
            Map map = subscribeService.editecho(row);
            return new Result(true, "预约信息回显成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

    /*编辑套餐信息的回显*/
    @RequestMapping("/setmealecho")
    public Result setmealecho(@RequestBody Map row) {
        try {
            Integer[] id = subscribeService.setmealecho(row);

            return new Result(true, "预约信息回显成功", id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

    /*编辑*/
    @RequestMapping("/editor")
    public Result editor(@RequestBody Map map, Integer[] id) {
        try {
            Result editor = subscribeService.editor(map, id);
            return editor;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }


    /*删除*/
    @RequestMapping("/dele")
    public Result dele(@RequestBody Map row) {

        try {
            subscribeService.dele(row);

            return new Result(true, "预约删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

    /*通过按钮修改状态*/
    @RequestMapping("/alterthestate")
    public Result alterthestate(@RequestBody Map state) {
        try {
            boolean value = (boolean) state.get("value");
            if (value == true) {
                state.put("orderStatus", "已到诊");
            } else {
                state.put("orderStatus", "未到诊");
            }
            subscribeService.alterthestate(state);
            return new Result(true, "状态设置完成");

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "状态设置失败");
        }
    }

}
